package com.nsoft.objects;

import java.util.TreeMap;

public class ChapterInfo {
	private int id;
	private String name;
	private TreeMap<Integer,
		TreeMap<Integer,PageInfo>> pageList;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}	

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public ChapterInfo(){
		pageList=new TreeMap<Integer,
				TreeMap<Integer,PageInfo>>();
	}
	
	public ChapterInfo(int chapIndex){
		id=chapIndex;
		pageList=new TreeMap<Integer,
				TreeMap<Integer,PageInfo>>();
	}

	public void addPage(PageInfo pInfo){
		int x=pInfo.gethIndex(),
				y=pInfo.getvIndex();
		TreeMap<Integer,PageInfo> _plist;
		if (pageList.containsKey(x)){
			_plist=pageList.get(x);
			_plist.put(y,pInfo);
		}else{
			_plist=new TreeMap<Integer,PageInfo>();
			_plist.put(y,pInfo);
			pageList.put(x,_plist);
		}
	}

	/**
	 * returns a 2-dimension list of <b>PageInfo</b>. 
	 * the list can contain a few null elements.
	 * please select only the non-empty elements.  
	 * @return
	 */
	public PageInfo[][] get2DPageList(){
		int maxKey=pageList.lastKey();
		PageInfo[][] pageList2D=new PageInfo[maxKey+1][];
		TreeMap<Integer,PageInfo> _plist=null;
		int subMaxKey=-1;
		PageInfo pInfo=null;
		for (int key:pageList.keySet().
					toArray(new Integer[]{})){
			_plist=pageList.get(key);
			subMaxKey=_plist.lastKey();
			pageList2D[key]=new PageInfo[subMaxKey+1];
			
			for (int subkey:_plist.keySet().
						toArray(new Integer[]{})){
				pInfo=_plist.get(subkey);
				pageList2D[key][subkey]=pInfo;
			}
		}
		return pageList2D;
	}
	
	/**
	 * this function probably returns null.
	 * @param x
	 * @param y
	 * @return
	 */
	public PageInfo getPage(int x,int y){
		PageInfo pInfo=null;		
		if (pageList.containsKey(x)){
			pInfo=pageList.get(x).get(y);
		}
		return pInfo;
	}

	
	
	/**
	 * return TRUE if element is found.  
	 * otherwise return FALSE
	 * @param x
	 * @return
	 */
	public boolean removeHIndex(int x){
		if (pageList.containsKey(x)){
			pageList.remove(x);
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * return TRUE if element is found. 
	 * otherwise return FALSE
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean removeVIndex(int x,int y){
		if (pageList.containsKey(x)){
			if (pageList.get(x).containsKey(y)){
				pageList.get(x).remove(y);
				return true;
			}
			return false;
		}
		return false;
	}
	
	public void removeAll(){
		pageList.clear();
	}
	
}
